<?php 

/**
 * 留言展示
 */

namespace app\admin\controller;

use think\Request;
use app\admin\model\Member;

class Villagemessages extends Base{

    public function _initialize() {
        parent::_initialize();
        $this->model = 'villagemessages';
        $this->defaultWhere = ['isDelete'=>2,'a.villageId'=>session('ADMIN')['villageId'],'responderId'=>'0']; //default where
        $this->defaultOrder = 'createDate DESC';
    }

    public function index() {
        $model = model($this->model);
        $request = $this->request;
        if($this->defaultWhere){
            $model = $model->where($this->defaultWhere);
        }
        if($this->defaultOrder){
            $model = $model->order($this->defaultOrder);
        }
        if ($this->pageSize) {
            $pageSize = $this->pageSize;
            if ($request->param('pageSize') && in_array($request->param('pageSize'), $pageSize)) {
                $pageSizeCurrent = $request->param('pageSize');
                setcookie($this->model . '_pageSize', $pageSizeCurrent, time() + 1000 * 86400, '/');
            } else {
                $pageSizeFromCookie = isset($_COOKIE[$this->model . '_pageSize']) ? $_COOKIE[$this->model . '_pageSize'] : '';
                if ($pageSizeFromCookie && in_array($pageSizeFromCookie, $pageSize)) {
                    $pageSizeCurrent = $pageSizeFromCookie;
                } else {
                    $pageSizeCurrent = reset($pageSize);
                }
            }
            $results = $model->alias('a')->join('user b','a.userId=b.id')->field('a.*,b.nickName,b.avatarUrl,b.mobile,b.isMember,b.memberId')->paginate($pageSizeCurrent);

            $hasPaginate = true;
            // Action url
            $pageSizeUrl = fullUrl($request->path());
        } else {
            $results = $model->alias('a')->join('user b','a.userId=b.id')->field('a.*,b.nickName,b.avatarUrl,b.mobile,b.isMember,b.memberId')->select();
            $hasPaginate = false;
        }
        foreach ($results as $k => $v) {
            if($v['isMember'] == 1) {  //本村村民
                $member = Member::where('id',$v['memberId'])->find();
                $results[$k]['nickName'] = $member['name'];
                $results[$k]['avatarUrl'] = $member['avatar'];
                $results[$k]['mobile'] = $member['mobile'];
            } else {
                $results[$k]['nickName'] = urldecode($v['nickName']);
            }
            $v->image;
            $results[$k]['tag'] = empty($v->replay) ? 'toreplay' : 'already';  //标识是否已回复，便于前台搜索
        }
        return $this->view->fetch($this->indexView, [
            'datas' => $results,
            'pageSize' => ['size' => $pageSize, 'current' => $pageSizeCurrent, 'url' => $pageSizeUrl],
            'hasPaginate' => $hasPaginate,
            'searchFields' => $this->searchFields
        ]);
    }

    //回复村留言
    public function addPost(Request $request, $redirect = '') {
        return parent::addPost($request,$redirect);
    }

    //删除留言或单条回复
    public function delete() {
        $request = $this->request;
        $id = $request->param('id');

        $model = model($this->model);
        $del = $model->where(['id'=>$id])->whereOr(['responderId'=>$id])->update(['isDelete'=>1]);
        if($del) {
            return $this->success('删除成功！', 'admin/' . strtolower($this->model) . '/index');
        } else {
            return $this->error('删除失败！', 'admin/' . strtolower($this->model) . '/index');
        }
    }


}